package com.gun.management.module.task.dal.mysql.taskRewardRecord;

import java.time.LocalDateTime;
import java.util.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.gun.management.framework.mybatis.core.mapper.BaseMapperX;
import com.gun.management.module.task.controller.app.vo.AppTaskAuditInfoRespVO;
import com.gun.management.module.task.dal.dataobject.taskRewardRecord.TaskRewardRecordDO;
import org.apache.ibatis.annotations.*;
import com.gun.management.module.task.controller.admin.taskRewardRecord.vo.*;

/**
 * 任务奖励记录信息 Mapper
 *
 * @author 测试账号
 */
@Mapper
public interface TaskRewardRecordMapper extends BaseMapperX<TaskRewardRecordDO> {

    default PageResult<TaskRewardRecordDO> selectPage(TaskRewardRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TaskRewardRecordDO>()
                .eqIfPresent(TaskRewardRecordDO::getTaskId, reqVO.getTaskId())
                .eqIfPresent(TaskRewardRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(TaskRewardRecordDO::getTaskScreenshot, reqVO.getTaskScreenshot())
                .eqIfPresent(TaskRewardRecordDO::getRewardScore, reqVO.getRewardScore())
                .betweenIfPresent(TaskRewardRecordDO::getRewardTime, reqVO.getRewardTime())
                .eqIfPresent(TaskRewardRecordDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(TaskRewardRecordDO::getAuditUserId, reqVO.getAuditUserId())
                .eqIfPresent(TaskRewardRecordDO::getAuditReason, reqVO.getAuditReason())
                .betweenIfPresent(TaskRewardRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(TaskRewardRecordDO::getId));
    }


    @Insert("INSERT INTO task_reward_record(`task_id`, `user_id`, task_screenshot,`reward_score`, `reward_time`,  `audit_status`, `audit_user_id`, `audit_reason`, `audit_time` ) VALUES " +
            "(#{entity.taskId}, #{entity.userId},#{entity.taskScreenshot}, #{entity.rewardScore}, #{entity.rewardTime},  #{entity.auditStatus}, #{entity.auditUserId}, #{entity.auditReason}, #{entity.auditTime} );")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insertAndGetId(@Param("entity") TaskRewardRecordDO entity);



    @Select({"<script> " +
            "select A.id,B.id task_id,B.name task_name,B.category_id,A.reward_score,A.user_id,C.nickname nick_name,CONCAT(LEFT(C.mobile, 3), '****', RIGHT(C.mobile, 4)) mobile,  " +
            " A.task_screenshot,A.reward_time,A.audit_time,A.audit_status,A.audit_reason,A.create_time,(select nickname from system_users M  where m.id = A.audit_user_id ) audit_user_name,A.update_time from " +
            " task_reward_record A INNER   JOIN  task_info B ON A.task_id = B.id  " +
            "LEFT JOIN member_user C on A.user_id = C.id   " +
            "<where>" +
            " A.deleted = 0 " +
            "<if test='id != null'>" +
            " and A.id =  #{id}  " +
            "</if>" +
            "</where>" +
            " order by A.create_time desc, A.id desc  " +
            "</script>"}
    )
    public  TaskRewardRecordRespVO  getTaskRewardRecordById(@Param("id") Long id);

    @Select({"<script>select ifnull(count(1),0)  times from  task_reward_log A   where A.task_id = #{taskId} and    A.user_id = #{userId}" +
            "<if test='createTime != null '>" +
            " and A.create_time  between  #{createTime[0]} and #{createTime[1]}  " +
            "</if>" +
            "  and A.deleted = 0</script>"}
    )
    public  Integer  getTaskRecords(@Param("taskId") Long taskId,@Param("userId") Long userId,@Param("createTime") LocalDateTime[]  createTime);

    @Select({"<script>select ifnull(reward_score,0)  sumRewardScore from  task_reward_log A   where  A.task_id = #{taskId}  and   A.user_id = #{userId}" +
            "<if test='createTime != null '>" +
            " and A.create_time  between  #{createTime[0]} and #{createTime[1]}  " +
            "</if>" +
            "  and A.deleted = 0 order by A.create_time desc limit 1 </script>"}
    )
    public  Integer  getUserTaskRewardScore(@Param("taskId") Long taskId,@Param("userId") Long userId,@Param("createTime") LocalDateTime[]  createTime);


    @Select({"<script>select ifnull(sum(reward_score),0)  sumRewardScore from  task_reward_log A   where  A.task_id = #{taskId}  and   A.user_id = #{userId}" +
            "<if test='createTime != null '>" +
            " and A.create_time  between  #{createTime[0]} and #{createTime[1]}  " +
            "</if>" +
            "  and A.deleted = 0 order by A.create_time desc limit 1 </script>"}
    )
    public  Integer  getUserSummaryTaskRewardScore(@Param("taskId") Long taskId,@Param("userId") Long userId,@Param("createTime") LocalDateTime[]  createTime);


    @Select({"<script> " +
            "select A.id,B.id task_id,B.name task_name,A.task_screenshot," +
            " A.audit_status,A.audit_reason,A.audit_time,(select nickname from system_users M  where m.id = A.audit_user_id ) audit_user_name,A.update_time from " +
            " task_reward_record A INNER   JOIN  task_info B ON A.task_id = B.id  " +
            "LEFT JOIN member_user C on A.user_id = C.id   " +
            "<where>" +
            " A.deleted = 0 " +
            "<if test='taskId != null'>" +
            " and B.id =  #{taskId}  " +
            "</if>" +
            "</where>" +
            " order by A.create_time desc, A.id desc limit 1  " +
            "</script>"}
    )
    public AppTaskAuditInfoRespVO getTaskAuditInfoById(@Param("taskId") Long taskId);


    @Select({"<script> " +
            "select A.id ,B.id task_id,B.name task_name, B.category_id,A.reward_score,A.user_id,C.nickname nick_name,CONCAT(LEFT(C.mobile, 3), '****', RIGHT(C.mobile, 4)) mobile,  " +
            " A.task_screenshot,A.reward_time,A.audit_time,A.audit_status,A.audit_reason,A.create_time,(select nickname from system_users M where m.id = A.audit_user_id ) audit_user_name,A.update_time from " +
            " task_reward_record A INNER   JOIN  task_info B  ON A.task_id = B.id  " +
            "LEFT JOIN member_user C on A.user_id = C.id   " +
            "<where>" +
            "  A.deleted = 0  " +
            "<if test='taskName != null  and taskName != \"\" '>" +
            " and B.name like   concat('%',#{taskName},'%') " +
            "</if>" +
            "<if test='categoryId != null'>" +
            " and B.category_id =  #{categoryId}  " +
            "</if>" +
            "<if test='auditStatus != null '>" +
            " and A.audit_status =  #{auditStatus}  " +
            "</if>" +
            "<if test='rewardTime != null '>" +
            " and A.reward_time  between  #{rewardTime[0]} and #{rewardTime[1]}  " +
            "</if>" +

            "<if test='userName != null  and userName != \"\" '>" +
            " and ( C.nickname like   concat('%',#{userName},'%') or A.user_id like   concat('%',#{userName},'%'))" +
            "</if>" +
            "</where>" +
            " order by A.create_time desc, A.id desc  " +
            "</script>"}
    )
    public IPage<TaskRewardRecordRespVO> selectTaskRewardRecordPageList(IPage<?> page, @Param("taskName") String  taskName , @Param("categoryId") Integer categoryId, @Param("auditStatus") Integer  auditStatus ,  @Param("rewardTime") LocalDateTime[] rewardTime,@Param("userName") String  userName );



}